package cn.com.gas.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import cn.com.common.constants.Constants;
import cn.com.common.result.DataResult;
import cn.com.gas.entity.GasAnalyseRegionRankingEntity;
import cn.com.gas.service.GasAnalyseRegionRankingService;
import cn.com.system.entity.SysDept;
import cn.com.system.service.DeptService;
import cn.com.system.service.impl.HttpSessionService;
import cn.com.util.TsBqUtils;



/**
 * 油站排行榜
 *
 * @author shaochangyang
 * @email l2l84o7o5@live.com
 * @date 2024-04-15 20:42:03
 */
@Controller
@RequestMapping("/gas/analyse/regionranking")
public class GasAnalyseRegionRankingController {
    @Autowired
    private GasAnalyseRegionRankingService gasAnalyseRegionRankingService;
    @Autowired
    private DeptService deptService;
    
    @Autowired
    private HttpSessionService httpSessionService;
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "查询分页数据")
    @PostMapping("/list")
    // @RequiresPermissions("gasAnalyseRegionRanking:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody GasAnalyseRegionRankingEntity gasAnalyseRegionRanking){
        Page page = new Page(gasAnalyseRegionRanking.getPage(), gasAnalyseRegionRanking.getLimit());
        LambdaQueryWrapper<GasAnalyseRegionRankingEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        if(TsBqUtils.isNotNull(gasAnalyseRegionRanking.getRegionName())){
			queryWrapper.like(GasAnalyseRegionRankingEntity::getRegionName,gasAnalyseRegionRanking.getRegionName());
		}
		
		if(TsBqUtils.isNotNull(gasAnalyseRegionRanking.getYearMonths())){
			queryWrapper.eq(GasAnalyseRegionRankingEntity::getYearMonths,gasAnalyseRegionRanking.getYearMonths());
		}
		queryWrapper.orderByAsc(GasAnalyseRegionRankingEntity::getRank);
        IPage<GasAnalyseRegionRankingEntity> iPage = gasAnalyseRegionRankingService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }
    
    
    @ApiOperation(value = "区域销售额统计按年月分组")
	@PostMapping("/groupYearMonth")
	@ResponseBody
	public DataResult groupYearMonth(@RequestBody GasAnalyseRegionRankingEntity gasAnalyseRegionRanking) {
    	
    	Date date = new Date();
    	String currentDeptId = httpSessionService.getCurrentDeptId();
    	
    	String taxorgcode = gasAnalyseRegionRanking.getTaxorgcode();
    	
    	String yearMonths = gasAnalyseRegionRanking.getYearMonths();
    	if(TsBqUtils.isNull(yearMonths)){
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            String currentYear = sdf.format(date);
            yearMonths = currentYear;
    	}
    	
    	String taxorgName = "";
    	if(TsBqUtils.isNotNull(taxorgcode)){
    		SysDept sysDept = deptService.getById(taxorgcode);
    		taxorgName =  sysDept.getName();
    		String taxOrgLevel = TsBqUtils.getTaxOrgLevel(taxorgcode);
    		String substring = "";
    		
    		if(Constants.TAX_COUNTES.equals(taxOrgLevel)){
    			// 县区局
    			substring = currentDeptId.substring(0,7);
    		}else if(Constants.TAX_CITIES.equals(taxOrgLevel)){
    			// 市局
    			substring = currentDeptId.substring(0,5);
    		}else{
    			// 省局
    		}
    		
            gasAnalyseRegionRanking.setTaxorgcode(substring);
    		
    		
    	}else{
    		String taxOrgLevel = TsBqUtils.getTaxOrgLevel(currentDeptId);
    		String substring = "";
    		
    		if(Constants.TAX_COUNTES.equals(taxOrgLevel)){
    			// 县区局
    			substring = currentDeptId.substring(0,7);
    		}else if(Constants.TAX_CITIES.equals(taxOrgLevel)){
    			// 市局
    			substring = currentDeptId.substring(0,5);
    		}else{
    			// 省局
    		}
            gasAnalyseRegionRanking.setTaxorgcode(substring);
    		
    	}
    	
		GasAnalyseRegionRankingEntity regionRanking = gasAnalyseRegionRankingService.groupYearMonth(gasAnalyseRegionRanking);

		List<Object> list = new ArrayList<>();

		List<String> regionRankingList = new ArrayList<>();
		if (TsBqUtils.isNotNull(regionRanking)) {
			regionRankingList.add(regionRanking.getMonth1());
			regionRankingList.add(regionRanking.getMonth2());
			regionRankingList.add(regionRanking.getMonth3());
			regionRankingList.add(regionRanking.getMonth4());
			regionRankingList.add(regionRanking.getMonth5());
			regionRankingList.add(regionRanking.getMonth6());
			regionRankingList.add(regionRanking.getMonth7());
			regionRankingList.add(regionRanking.getMonth8());
			regionRankingList.add(regionRanking.getMonth9());
			regionRankingList.add(regionRanking.getMonth10());
			regionRankingList.add(regionRanking.getMonth11());
			regionRankingList.add(regionRanking.getMonth12());
			
		}


		List<String> monthList = new ArrayList<>();
		monthList.add("1月");
		monthList.add("2月");
		monthList.add("3月");
		monthList.add("4月");
		monthList.add("5月");
		monthList.add("6月");
		monthList.add("7月");
		monthList.add("8月");
		monthList.add("9月");
		monthList.add("10月");
		monthList.add("11月");
		monthList.add("12月");
		
		list.add(regionRankingList);
		list.add(monthList);
		list.add(yearMonths + taxorgName + "区域销售额统计");
		
		return DataResult.success(list);
	}
    
    

}
